#define JMBC_INSIDE
#include "ilTGA.h"
#include "ilImage.h"
#include <Math/mlMath.h>
#include <Core/clCore.h>
#include <stdio.h>
#include <GL/glu.h>

void
ilImageLoadTGA(unsigned int id, unsigned int filepath)
{
	FILE *stream;
	char tmp[256];

	//stream = _wfopen(tlStringData(filepath), L"rb");
	wcstombs(tmp, tlStringData(filepath), 256);
	stream = fopen(tmp, "rb");

	if(stream)
	{
		const unsigned char tgamagic[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		unsigned char magic[12];

		fread(magic, 1, sizeof(magic), stream);

		if(!memcmp(tgamagic, magic, 12 * sizeof(unsigned char)))
		{
			unsigned char header[6];

			fread(header, 1, 6 * sizeof(unsigned char), stream);

			if(header[4] == 24 || header[4] == 32)
			{
				ILImage *image;
				unsigned int width, height, i;

				image = ilImageStore(id);

				width = (unsigned int) (header[1] << 8) + header[0];
				height = (unsigned int) (header[3] << 8) + header[2];

				image->width = mlNextTwoPower((float) width);
				image->height = mlNextTwoPower((float) height);
				image->bpp = header[4] >> 3;
				image->pixels = malloc(image->width * image->height * image->bpp * sizeof(unsigned char));

				fread(image->pixels, 1, image->width * image->height * image->bpp, stream);
				for(i = 0; i < image->width * image->height * image->bpp; i += image->bpp)
				{
					GLubyte swap;
					swap = image->pixels[i];
					image->pixels[i] = image->pixels[i + 2];
					image->pixels[i + 2] = swap;
				}

				if(width != image->width || height != image->height)
					gluScaleImage(ilImageBPPToFormat(image->bpp), width, height, GL_UNSIGNED_BYTE, image->pixels, image->width, image->height, GL_UNSIGNED_BYTE, image->pixels);
			}
			else
				elErrorSet(EL_ERROR_INVALID);
		}
		else
			elErrorSet(EL_ERROR_INVALID);

		fclose(stream);
	}
	else
		elErrorSet(EL_ERROR_INVALID);
}

void
ilImageSaveTGA(unsigned int id, unsigned int filepath)
{
	FILE *stream;
	char tmp[256];

	//stream = _wfopen(tlStringData(filepath), L"wb");
	wcstombs(tmp, tlStringData(filepath), 256);
	stream = fopen(tmp, "wb");

	if(stream)
	{
		ILImage *image;
		const unsigned char magic[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		unsigned char header[6], *pixels;
		unsigned int i;

		image = ilImageStore(id);

		fwrite(magic, 1, sizeof(magic), stream);

		header[0] = image->width & 0xFF;
		header[1] = (image->width >> 8);
		header[2] = image->height & 0xFF;
		header[3] = (image->height >> 8);
		header[4] = image->bpp << 3;
		header[5] = 0;
		fwrite(header, 1, sizeof(header), stream);

		pixels = malloc(image->width * image->height * image->bpp * sizeof(unsigned char));
		memcpy(pixels, image->pixels, image->width * image->height * image->bpp);
		for(i = 0; i < image->width * image->height * image->bpp; i += image->bpp)
		{
			GLubyte swap;
			swap = pixels[i];
			pixels[i] = pixels[i + 2];
			pixels[i + 2] = swap;
		}
		fwrite(pixels, 1, image->width * image->height * image->bpp, stream);
		free(pixels);

		fclose(stream);
	}
	else
		elErrorSet(EL_ERROR_INVALID);
}
